package main;

import com.github.crab2died.ExcelUtils;
import com.github.crab2died.sheet.wrapper.NoTemplateSheetWrapper;
import model.statical.Model;
import model.dynamic.DyADepartment;
import model.dynamic.DyBDepartment;
import model.dynamic.DyCDepartment;
import model.dynamic.DyDDepartment;
import model.dynamic.DyModel;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class DynamicMain
{

	public static void main(String[] args)
	{
		List<DyModel> actualList, authorizedList = new ArrayList<>();
		try
		{
			actualList = ExcelUtils.getInstance().readExcel2Objects("数据模板/动态数据.xlsx",
					DyModel.class,1);
			List<Model> models = ExcelUtils.getInstance()
					.readExcel2Objects("数据模板/动态数据.xlsx", Model.class, 0);
			for (Model model : models)
				authorizedList.add(new DyModel(model));
		} catch (Exception e)
		{
			e.printStackTrace();
			return;
		}

		calculate(actualList, authorizedList);
	}

	public static void calculate(File file)
	{
		List<DyModel> actualList, authorizedList = new ArrayList<>();
		try(InputStream inputStream = new FileInputStream(file))
		{
			actualList = ExcelUtils.getInstance().readExcel2Objects(inputStream,
					DyModel.class, 1);
		} catch (Exception e)
		{
			e.printStackTrace();
			return;
		}
		
		try(InputStream inputStream = new FileInputStream(file))
		{
			List<Model> models = ExcelUtils.getInstance()
					.readExcel2Objects(inputStream, Model.class, 0);
			for (Model model : models)
				authorizedList.add(new DyModel(model));
		} catch (Exception e)
		{
			e.printStackTrace();
			return;
		}

		calculate(actualList, authorizedList);
	}

	public static void calculate(List<DyModel> actualList,
			List<DyModel> authorizedList)
	{
		ArrayList<DyDDepartment> allDyDDepartments = new ArrayList<>();
		DyDDepartment tempDyDDepartment = null;
		for (DyModel model : authorizedList)
		{
			if (tempDyDDepartment == null
					|| !tempDyDDepartment.isModelInThisDDepartment(model))
			{
				tempDyDDepartment = new DyDDepartment(model);
				allDyDDepartments.add(tempDyDDepartment);
			}
			tempDyDDepartment.notMatchAuthorizedModels.add(model);
		}

		for (DyModel model : actualList)
		{
			for (DyDDepartment dyDDepartment : allDyDDepartments)
			{
				if (dyDDepartment.isModelInThisDDepartment(model))
				{
					dyDDepartment.notMatchActualModels.add(model);
					break;
				}
			}
		}

		System.out.println("D级单位总数量:" + allDyDDepartments.size());

		for (DyDDepartment dyDDepartment : allDyDDepartments)
			dyDDepartment.calculateStatisticData();

		List<DyCDepartment> allDyCDepartments = new ArrayList<>();
		DyCDepartment tempDyCDepartment = null;
		for (DyDDepartment dyDDepartment : allDyDDepartments)
		{
			if (tempDyCDepartment == null
					|| !tempDyCDepartment.isSubDepartmentInThis(dyDDepartment))
			{
				tempDyCDepartment = new DyCDepartment(dyDDepartment);
				allDyCDepartments.add(tempDyCDepartment);
			}
			tempDyCDepartment.addSubDepartment(dyDDepartment);
		}

		System.out.println("C级单位总数量:" + allDyCDepartments.size());
		for (DyCDepartment dyCDepartment : allDyCDepartments)
			dyCDepartment.calculateStatisticData();

		List<DyBDepartment> allDyBDepartments = new ArrayList<>();
		DyBDepartment tempDyBDepartment = null;
		for (DyCDepartment dyCDepartment : allDyCDepartments)
		{
			if (tempDyBDepartment == null
					|| !tempDyBDepartment.isSubDepartmentInThis(dyCDepartment))
			{
				tempDyBDepartment = new DyBDepartment(dyCDepartment);
				allDyBDepartments.add(tempDyBDepartment);
			}
			tempDyBDepartment.addSubDepartment(dyCDepartment);
		}

		System.out.println("B级单位总数量:" + allDyBDepartments.size());
		for (DyBDepartment dyBDepartment : allDyBDepartments)
			dyBDepartment.calculateStatisticData();

		List<DyADepartment> allDyADepartments = new ArrayList<>();
		DyADepartment tempDyADepartment = null;
		for (DyBDepartment dyBDepartment : allDyBDepartments)
		{
			if (tempDyADepartment == null
					|| !tempDyADepartment.isSubDepartmentInThis(dyBDepartment))
			{
				tempDyADepartment = new DyADepartment(dyBDepartment);
				allDyADepartments.add(tempDyADepartment);
			}
			tempDyADepartment.addSubDepartment(dyBDepartment);
		}

		System.out.println("A级单位总数量:" + allDyADepartments.size());
		for (DyADepartment dyADepartment : allDyADepartments)
			dyADepartment.calculateStatisticData();

		List<NoTemplateSheetWrapper> sheets = new ArrayList<>();
		sheets.add(new NoTemplateSheetWrapper(allDyDDepartments,
				DyDDepartment.class, true, "D级单位统计数据"));
		sheets.add(new NoTemplateSheetWrapper(allDyCDepartments,
				DyCDepartment.class, true, "C级单位统计数据"));
		sheets.add(new NoTemplateSheetWrapper(allDyBDepartments,
				DyBDepartment.class, true, "B级单位统计数据"));
		sheets.add(new NoTemplateSheetWrapper(allDyADepartments,
				DyADepartment.class, true, "A级单位统计数据"));

		try
		{
			ExcelUtils.getInstance().noTemplateSheet2Excel(sheets,
					"动态统计数据.xlsx");
			MainFrame.openFile(new File("动态统计数据.xlsx"));
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
